﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VastPark.ID.Design;
using VastPark.ID;
using VastPark.ID.Policy.Cryptography;

namespace VastParkIDPlugin
{
    class LoginProvider : ILoginProvider
    {
        private static ProfileService _ProfileService;

        public static ProfileService ProfileService
        {
            get { return _ProfileService; }
        }
     

        public LoginProvider()
        {
            var serviceConnector = new Metaforik.Design.MetaforikServiceConnector(new Uri("http://id.vastpark.com/VastParkWS/"), "ImmlPad", new Version(1,0));
            _ProfileService = new ProfileService(serviceConnector, new Sha256PasswordHashingAlgorithm());
        }

        #region ILoginProvider Members

        public IServiceResponse<VastPark.ID.User> CreateUser(VastPark.ID.ServiceRequest<VastPark.ID.User> request)
        {
            throw new NotImplementedException();
        }

        public string HashPassword(string password)
        {
            return _ProfileService.HashPassword(password);
        }

        public bool IsLoggedIn { get; private set; }

        public void Login(string username, string password, bool passwordHashed, VastPark.ID.ResponseCallback<VastPark.ID.User> resultHandler)
        {
            var result = _ProfileService.Login(username, password, passwordHashed);
            resultHandler(result);
        }

        public IServiceResponse<VastPark.ID.User> Login(string username, string password, bool passwordHashed)
        {
            try
            {
                return _ProfileService.Login(username, password, passwordHashed);
            }
            catch
            {
                return new ServiceResponse<User>(null, new ErrorMessage("Login failed, unable to contact the host.", 0, DateTime.Now));
            }
        }

        public void Logout()
        {
            _ProfileService.Logout();
        }

        public IServiceResponse<object> ResetPassword(string email, string username)
        {
            return _ProfileService.ResetPassword(email, username);
        }

        public VastPark.ID.User UserProfile
        {
            get { return _ProfileService.User; }
        }

        #endregion
    }
}
